package com.hundsun.fcp.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.hundsun.fcp.R;
import com.hundsun.fcp.base.BaseActivity;
import com.hundsun.fcp.common.Redirect;
import com.hundsun.fcp.common.utils.StringUtils;
import com.hundsun.fcp.widget.EditTextWithClear;
import com.hundsun.fcp.widget.TimerButton;

public class MineResetLoginPwdActivity extends BaseActivity implements OnClickListener {
	private EditTextWithClear mUsername;
	private EditTextWithClear mSmscode;
	private EditTextWithClear mNewPwd;
	private EditTextWithClear mConfirmPwd;
	private TimerButton mTimeButton;
	private Button mSubmit;
	
	private String strUserName;
	private String strSmsCode;
	private String strNewPwd;
	private String strConfirmPwd;
	
	protected int getContentLayout() {
		return R.layout.fcp_mine_reset_loginpwd;
	}
	@Override
	protected void initHeader() {
		super.initHeader();
		mHeaderMenu.setTitle(R.string.mine_head_reset_loginpwd);
	}
	@Override
	public void initView() {
		super.initView();
		mUsername = (EditTextWithClear) findViewById(R.id.et_username);
		mSmscode = (EditTextWithClear) findViewById(R.id.id_msgcode_modify_ipt);
		mNewPwd = (EditTextWithClear) findViewById(R.id.et_newpwd);
		mConfirmPwd = (EditTextWithClear) findViewById(R.id.et_confirmpwd);
		mTimeButton = (TimerButton) findViewById(R.id.id_obtain_msgcode_modify_btn);
		mSubmit = (Button) findViewById(R.id.btn_submit);
		
		mTimeButton.setOnClickListener(this);
		mSubmit.setOnClickListener(this);
		
		mUsername.addTextChangedListener(new TextWatcher() {
			
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				
			}
			
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}
			
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				setMsgCodeBtnEnabled();
				vfySignBtn();
			}
		});
		mSmscode.addTextChangedListener(mTextWatcher);
		mNewPwd.addTextChangedListener(mTextWatcher);
		mConfirmPwd.addTextChangedListener(mTextWatcher);
		
		
	}
	public void onClick(View v) {
		if(v == mTimeButton){
			// 已经验证非空 可直接使用
			strUserName = mUsername.getText().toString().trim();
			//TODO doObtainSms();
		}else if(v == mSubmit){
			// 已经验证非空 可直接使用
			strUserName = mUsername.getText().toString().trim();
			strSmsCode = mSmscode.getText().toString().trim();
			strNewPwd = mNewPwd.getText().toString().trim();
			strConfirmPwd = mConfirmPwd.getText().toString().trim();
			if(strNewPwd.equals(strConfirmPwd)){
				//TODO doSubmit();
			}else{
				// 显示错误信息
				Redirect.makeToast(MineResetLoginPwdActivity.this,
						R.string.pwd_confirm_error);
			}
		}
	}
	
	private TextWatcher mTextWatcher = new TextWatcher() {
		
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			
		}
		
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			
		}
		
		public void afterTextChanged(Editable s) {
			vfySignBtn();
		}
	};
	/**
	 * 根据用户名是否为空来控制 获取验证码 按钮
	 */
	private void setMsgCodeBtnEnabled() {
		strUserName = mUsername.getText().toString().trim();
		if (strUserName.length() > 0) {
			mTimeButton.setEnabled(true);
			mTimeButton.setBackgroundResource(R.color.msgcode_bg);
		} else {
			mTimeButton.setEnabled(false);
			mTimeButton.setBackgroundResource(R.color.signup_msgcode_bg);
		}
	}

	/**
	 * 根据是否输入用户名、验证码、新密码和确认新密码来控制提交按钮
	 */
	private void vfySignBtn() {
		strUserName = mUsername.getText().toString().trim();
		strSmsCode = mSmscode.getText().toString().trim();
		strNewPwd = mNewPwd.getText().toString().trim();
		strConfirmPwd = mConfirmPwd.getText().toString().trim();

		if (StringUtils.isEmpty(strUserName) || StringUtils.isEmpty(strSmsCode)
				|| StringUtils.isEmpty(strNewPwd)
				|| StringUtils.isEmpty(strConfirmPwd)) {
			mSubmit.setEnabled(false);
			mSubmit.setBackgroundResource(R.drawable.btn_grey_unable);
		} else {
			mSubmit.setEnabled(true);
			mSubmit.setBackgroundResource(R.drawable.btn_red_normal);
		}
	}

}
